﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers ;
using Lxzhu.OpenGL;
namespace Tutorials
{
    public class TimedGlutEngine:GlutEngine
    {
        public TimedGlutEngine()
        {
            this.Interval = 100;
        }
        protected Timer mTimer;
        public double Interval { get; set; }
        public long TotalFrames { get; private set; }
        public override void OnInit()
        {
            base.OnInit();
            mTimer = new Timer(this.Interval);
            mTimer.Elapsed += new ElapsedEventHandler(OnTimeElapsed);
            this.TotalFrames = 0;
            mTimer.Start();            
        }
        void OnTimeElapsed(object sender, ElapsedEventArgs e)
        {
            if (this.TotalFrames == long.MaxValue)
                this.TotalFrames = 0;
            this.TotalFrames += 1;
            OnTimeOut(sender, e);
            Glut.glutPostRedisplay();
        }
        public virtual void OnTimeOut(object sender, ElapsedEventArgs e)
        {
        }
        public override void OnExit()
        {
            base.OnExit();
            if (mTimer != null)
                mTimer.Stop();
        }
    }
}
